﻿
using System.Collections.Generic;
using WooEngine.Utility.Logger;
using WooEngine.Utility.Security.Base;

namespace WooEngine.ConfigWork
{
    public class ConfigFileData
    {
        private ConfigFileType type = ConfigFileType.DATA;

        private Dictionary<string, EncryptInt> key_dic;
        private JSONObject[] data;

        public ConfigFileData(JSONObject jSONObject)
        {
            if (jSONObject != null)
            {
                this.initWithJson(jSONObject);
            }
        }

        private void initWithJson(JSONObject jSONObject)
        {
            this.data = jSONObject.GetField("data").list.ToArray();
            this.type = (ConfigFileType)jSONObject.GetField("type").n;
            if (this.type == ConfigFileType.DATA)
            {
                List<string> list = jSONObject.GetField("key").keys;
                List<JSONObject> indexList = jSONObject.GetField("key").list;
                key_dic = new Dictionary<string, EncryptInt>();
                for(int i = 0; i < list.Count; i ++)
                {
                    key_dic.Add(list[i], (EncryptInt)indexList[i].n);
                }
            }
            else if(this.type == ConfigFileType.LANG)
            {

                List<string> list = jSONObject.GetField("data").keys;
                key_dic = new Dictionary<string, EncryptInt>();
                for (int i = 0; i < list.Count; i++)
                {
                    key_dic.Add(list[i], i);
                }
                
            }
        }

        public int Size {
            get{
                return this.data.Length;
            }
        }

        public JSONObject[] Data
        {
            get
            {
                return this.data;
            }
        }

        public Dictionary<string, EncryptInt> Key_dic
        {
            get
            {
                return this.key_dic;
            }
        }

        public JSONObject GetValue(string ID, int index = -1)
        {
            if (this.type == ConfigFileType.DATA && index >= 0)
            {
                int dIndex = this.key_dic[ID];
                if (dIndex >= 0)
                {
                    var list = this.data[dIndex];
                    if (list && list.Count > index)
                    {
                        return list[index];
                    }
                    else
                    {

                        WooLogger.WarnAppendString(" index is out of range ", index, " id ", ID);
                        return null;
                    }
                }
                else
                {
                    WooLogger.WarnAppendString(" dIndex is wrong ", dIndex, "id is ", ID);
                    return null;
                }

            }
            else if(this.type == ConfigFileType.LANG)
            {
                if (!key_dic.ContainsKey(ID))
                    return null;
                int dIndex = this.key_dic[ID];
                if (dIndex >= 0 && this.data.Length > dIndex)
                {
                    return this.data[dIndex];
                }
                else
                {
                    WooLogger.WarnAppendString(" dIndex is wrong ", dIndex, "id is ", ID);
                    return null;
                }
            }
            else
            {
                return this.data[int.Parse(ID)];
            }
        }


    }

}
